package thinking.in.spring.boot.samples.ienable.beandefinitionRegistrar;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import thinking.in.spring.boot.samples.ienable.selctor.ServerImportSelector;

import java.util.stream.Stream;

public class ServerImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        ServerImportSelector importSelector = new ServerImportSelector();
        String[] selectImports = importSelector.selectImports(annotationMetadata);

        Stream.of(selectImports)
                .map(BeanDefinitionBuilder::genericBeanDefinition)
                .map(BeanDefinitionBuilder::getBeanDefinition)
                .forEach(beanDefinition ->
                        BeanDefinitionReaderUtils
                                .registerWithGeneratedName(
                                        beanDefinition,
                                        registry));

    }
}
